package id.laskarmedia.opnvpn_flutter.handler.OC;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import id.laskarmedia.opnvpn_flutter.util.BeanConvertUtil;
import id.laskarmedia.opnvpn_flutter.bean.ServerConfig;
import id.laskarmedia.opnvpn_flutter.enums.VPNEventEnum;
import id.laskarmedia.opnvpn_flutter.handler.AbstractEventHandler;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * Concrete implementation of VPNEventHandler for handling VPN connection events.
 */
public class ConnectEventHandler extends AbstractEventHandler {

    /**
     * Returns the VPN event type handled by this event handler.
     *
     * @return The VPNEventEnum.CONNECT representing the VPN connection event.
     */
    @Override
    public VPNEventEnum getVPNEvent() {
        return VPNEventEnum.CONNECT;
    }

    /**
     * Handles the method call for VPN connection event.
     *
     * @param call   The method call received from Flutter.
     * @param result The result object to send the method call result back to Flutter.
     */
    @Override
    public void handler(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        ServerConfig config = BeanConvertUtil.converServerConfig(call);
        if (config == null || TextUtils.isEmpty(config.getOvpn())) {
            result.error("-2", "OpenVPN Config is required", "");
            return;
        }
        OPEN_VPN_ENGINEER.setServerConfig(config);
        OPEN_VPN_ENGINEER.prepareVpn();
        OPEN_VPN_ENGINEER.startVpn();
    }
}
